#include "CGprsEngine.h"

const TInt KMaxWapBearerLength = 64;


CGprsEngine* CGprsEngine::NewL(TUint& aIAP, RSocketServ &aSocketServ)
{
	CGprsEngine * self = new (ELeave) CGprsEngine (aIAP,aSocketServ);
	CleanupStack::PushL( self );
	self->ConstructL();
	CleanupStack::Pop();
	return self;		
}

CGprsEngine::CGprsEngine(TUint& aIAP, RSocketServ& aSocketServ):iGprsIAP(aIAP),iSocketServ(aSocketServ)
{
	
}

CGprsEngine::~CGprsEngine()
{
	this->CloseConnection();
	iSocketServ.Close();
	
	#ifdef __LOGGER
		iLog.Write(_L("CGprs destroyed."));
		iLog.CloseLog();
		iLog.Close();
	#endif
	
}

void CGprsEngine::ConstructL()
{
	#ifdef __LOGGER
	  	iLog.Connect();
		iLog.CreateLog(_L("findme"),_L("CCGprsEngine.txt"),EFileLoggingModeOverwrite);
	#endif	
	
	User::LeaveIfError(iSocketServ.Connect());
  	iState = EGprsIdle;	
  	
	iPref.SetIapId(iGprsIAP);
	iPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
	iPref.SetDirection( ECommDbConnectionDirectionOutgoing );
	#ifdef __LOGGER
		iLog.WriteFormat(_L("IAP from config = %d"),iGprsIAP);
	#endif
}

TInt CGprsEngine::OpenConnection()
{
	if (iState == EGprsIdle)
	{
		TInt err;
		err = iConnection.Open(iSocketServ);
		if ( err != KErrNone ) {
			return err;
		}
		TRequestStatus aStatus;
		iConnection.Start(iPref,aStatus);
		User::WaitForRequest(aStatus);
		if (aStatus == KErrNone)
		{
			#ifdef __LOGGER
				iLog.Write(_L("Connection GPRS created."));
			#endif
			iState=EGprsConnected;
			return KErrNone;
		}
		#ifdef __LOGGER
			iLog.WriteFormat(_L("ERROR: Connection GPRS failed : %d"), aStatus);
		#endif
		iConnection.Close();
		return KErrCouldNotConnect;		
	}
	#ifdef __LOGGER
		iLog.Write(_L("ERROR: Already connected."));
	#endif
	return KErrCouldNotConnect;	
}

void CGprsEngine::CloseConnection()
{
	if (iState == EGprsConnected)
	{
		iConnection.Stop();
  		iConnection.Close();
		iState=EGprsIdle;
		#ifdef __LOGGER
			iLog.Write(_L("Connection GPRS closed."));
		#endif
		return;	
	}
}

TBool CGprsEngine::IsConnected()
{
	if ( iState == EGprsIdle) return EFalse;
	else return ETrue;
}


void CGprsEngine::DataTransferredRequest(TUint& iUploadCounter, TUint& iDownloadCounter)
{
	if (iState == EGprsConnected)
	{
		TUint connectionuplinkVolume(0);
		TUint connectiondownlinkVolume(0);
		TPckg<TUint> connectionUplinkVolume(connectionuplinkVolume);
		TPckg<TUint> connectionDownlinkVolume(connectiondownlinkVolume);
		
		TRequestStatus status;
		iConnection.DataTransferredRequest(connectionUplinkVolume, connectionDownlinkVolume, status);
		User::WaitForRequest(status);
		
		iUploadCounter = connectionuplinkVolume;
		iDownloadCounter = connectiondownlinkVolume;
		
		#ifdef __LOGGER
			iLog.Write(_L("Ini info"));
			iLog.WriteFormat(_L("Uplink: %d "),connectionuplinkVolume);
			iLog.WriteFormat(_L("Down: %d "),connectiondownlinkVolume);
			iLog.Write(_L("Fin Info"));
		#endif
	}
}

void CGprsEngine::DataTransferredRequest(TDes8& iUploadCounter, TDes8& iDownloadCounter)
{
	if (iState == EGprsConnected)
	{
		TUint connectionuplinkVolume(0);
		TUint connectiondownlinkVolume(0);
		TBuf<32> iUp;
		TBuf<32> iDown;
		
		DataTransferredRequest(connectionuplinkVolume,connectiondownlinkVolume);
		
		iUp.Num(connectionuplinkVolume);
		iDown.Num(connectiondownlinkVolume);
		
		iUploadCounter.Zero();
		iDownloadCounter.Zero();
		iUploadCounter.Copy(_L("0"));
		iDownloadCounter.Copy(_L("0"));
		
		if (iUp.Length() <= iUploadCounter.MaxLength())
		{
			iUploadCounter.Copy(iUp);
		}
		
		if (iDown.Length() <= iDownloadCounter.MaxLength())
		{
			iDownloadCounter.Copy(iDown);
		}
	}
}